/**
 * @fileOverview
 * The worker code of WashizuMahjong.
 * http://code.google.com/p/washizu-mahjong/
 */
/**
 * The players who want to agari the discard tile.
 */
var agariList = [], 
pongOrKongList = [],
chowList = [],
sentPlayers = [],
ActionMessage = {"PickUp":"Pick Up","AddTile":"Add Tile into Hand","Kong":"Kong","Chow":"Chow","Pong":"Pong","Discard":"Discard","AddDora":"AddDora","Richi":"Richi","ReciveScore":"Recive Score","PayScore":"Pay Score","NewRound":"New Round","AbortiveDraw":"Abortive Draw","DrawRound":"Draw Round","Ron":"Picking Up a Discard","SelfDrawn":"Self-Drawn"};

/**
 * @class
 */
function Result(player){
	this.player = player;
}

this.addEventListener("message", function (e) {
	var data = e.data;
	sentPlayers.push(data.player);
	switch(data.action){
		case ActionMessage.Ron: 
			agariList.push(data.player);
			//TODO check who can agari.
			//sort the list
			agariList.sort(function(p1,p2){
				var n = data.dicardTile.owner.id;
				var p1ID = p1.id, p2ID = p2.id
				if(p1ID < n)
					p1ID += 2 * agariList.length;
				if(p2ID < n)
					p2ID += 2 * agariList.length;
				return p1ID-p2ID
			});
			//TODO send the result.
			break;
		case ActionMessage.Kong:
		case ActionMessage.Pong:
			if(agariList.length > 0)
				break;
			pongOrKongList.push(e.data.player);
			break;
		case ActionMessage.Chow:
			console.log(e.data.numOfPlayers)
			break;
	}
	if(sentPlayers.length){
		sendMessage()
	}
}, false);

function sendMessage(){
	
}
